﻿@page "/Production/CardSwipings"
@using BootstrapAdmin.Web.Core.Services.Production;
@using BootstrapAdmin.Web.ViewModels.Production;
@inject ICardSwipingService cardSwipingService

<AdminTable TItem="CardSwiping" IsMultipleSelect="true" IsPagination="true" PageItemsSource="PageItemsSource"
    ShowDefaultButtons="false" ShowExtendButtons="false"
    ShowSearch="true" SearchMode="SearchMode.Top" CustomerSearchModel="CardSwipingSearchModel"
    OnQueryAsync="@OnQueryAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.CardNo"></TableColumn>
        <TableColumn @bind-Field="@context.Alarm"></TableColumn>
        <TableColumn @bind-Field="@context.CreatedAt"></TableColumn>
    </TableColumns>
    <CustomerSearchTemplate>
        @if (context is CardSwipingSearchModel searchModel)
        {
            <div class="row g-3 form-inline">
                <div class="col-sm-6 col-md-6">
                    <BootstrapInput @bind-Value="@searchModel.CardNo" ShowLabel="true" />
                </div>
                <div class="col-sm-6 col-md-6">
                    <BootstrapInput @bind-Value="@searchModel.Alarm" ShowLabel="true" />
                </div>
                <div class="col-sm-6 col-md-6">
                    <DateTimeRange @bind-Value="@searchModel.CreatedTime" ShowLabel="true" AllowNull="true" />
                </div>
            </div>
        }
    </CustomerSearchTemplate>
</AdminTable>

@code {
    private static IEnumerable<int> PageItemsSource => new int[] { 20, 50, 100 };

    private CardSwipingSearchModel CardSwipingSearchModel = new();

    private async Task<QueryData<CardSwiping>> OnQueryAsync(QueryPageOptions options)
    {
        var filter = new CardSwipingFilter
        {
            CardNo = CardSwipingSearchModel.CardNo,
            Alarm = CardSwipingSearchModel.Alarm,
            CreatedTimeStart = CardSwipingSearchModel.CreatedTime?.Start,
            CreatedTimeEnd = CardSwipingSearchModel.CreatedTime?.End,
        };

        var (items, count) = await cardSwipingService.GetPagedAsync(filter, options.PageIndex, options.PageItems);

        return new QueryData<CardSwiping>()
        {
            Items = items,
            TotalCount = count,
            IsSorted = true,
            IsFiltered = true,
            IsSearch = true,
            IsAdvanceSearch = true,
        };
    }
}
